# Unix/Linux Makefile for 2D Clonal Mosaic Simulator
# Created by César Pastorini at Jan 17 2013

# Principal modificação: compilador de g++ para gcc. Isso deve resolver os problemas de seg.faults

EXEC        = cm
CFILES      = $(wildcard *.c)
CC          = gcc
INC_PATH    = 
CFLAGS      = 
DEFINES     = 
LLDLIBS     = -L./lib_voronoi2d -lVoronoi -lm

ENABLE_CUDA = yes
CUDA_FILES  = 
CUDA_PATH   = /usr/local/cuda
NVCC        = $(CUDA_PATH)/bin/nvcc

all: libVoronoi.a $(EXEC)

libVoronoi.a:
	cd lib_voronoi2d && $(MAKE)

ifeq ($(ENABLE_CUDA),yes)

CUDA_FILES  = $(wildcard *.cu)
DEFINES    += -DENABLE_CUDA
INC_PATH   += -I$(CUDA_PATH)/include
LLDLIBS    += -L$(CUDA_PATH)/lib -lcudart
CFLAGS     += -g

$(CUDA_FILES:.cu=.o): $(CUDA_FILES)
	@echo Building CUDA source file: $@
	$(NVCC) -o $@ -c $(CFLAGS) $(INC_PATH) $(LLDLIBS) $(DEFINES) $<

else

CFLAGS += -ggdb

endif

$(EXEC): $(CFILES:.c=.o) $(CUDA_FILES:.cu=.o)
ifeq ($(ENABLE_CUDA),yes)
	@echo Building final executable with CUDA support: $@
	$(NVCC) -o $@ $(CFLAGS) $(INC_PATH) $(LLDLIBS) $(DEFINES) $+
else
	@echo Building final executable without CUDA support: $@
	$(CC) $+ -o $@ $(CFLAGS) $(LLDLIBS) $(DEFINES) $(INC_PATH)
endif

%.o: %.c
	@echo Building C source file: $@
	$(CC) -o $@ -c $(CFLAGS) $(INC_PATH) $(LLDLIBS) $(DEFINES) $<

clean:
	-rm -f *.o

# Clean subdirectories of libVoronoi too
rclean: clean
	cd lib_voronoi2d && $(MAKE) rclean
